/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package eu.ict_omelette.mashup.compositionminer;

import eu.ict_omelette.mashup.adapters.pipes.PipesAdapter;
import eu.ict_omelette.mashup.canonicalmodel.CanonicalMashup;
import java.io.File;
import java.net.URI;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author carlos
 */
public class Main {
    private static final String PARAMETERVALUE_PATTERN = "pvp";
    private static final String CONNECTOR_PATTERN = "connp";
    private static final String CONNECTOR_COOCCURRENCE_PATTERN = "conncop";
    private static final String COMPONENT_COOCURRENCE_PATTERN = "compcop";

    private static String pattern = "";
    private static String din = "";
    private static String dout = "";
    private static double minsupdf = -1;
    private static double minsupdm = -1;
    private static double minsuppv = -1;

    
    public static void main(String args[]){
        if(args.length == 0){
            printProgramInfo();         
            System.exit(0);
        }else if(args.length % 2 != 0){
            System.out.println("Error: wrong list of parameters.\n");
            printProgramInfo();
            System.exit(1);
        }

        for(int i = 0; i < args.length; i++){
            if(args[i].equals("-p")){
                i++;
                pattern = args[i];
            }else if(args[i].equals("-din")){
                i++;
                din = args[i];
            }else if(args[i].equals("-dout")){
                i++;
                dout = args[i];
            }else if(args[i].equals("-minsupdf")){
                i++;
                minsupdf = Double.valueOf(args[i]);
            }
            else if(args[i].equals("-minsupdm")){
                i++;
                minsupdm = Double.valueOf(args[i]);
            }else if(args[i].equals("-minsuppv")){
                i++;
                minsuppv = Double.valueOf(args[i]);
            }else{
                System.out.println("Error: wrong list of parameters!\n");
                printProgramInfo();
                System.exit(1);
            }

        }

        if(din.equals("") || dout.equals("") || pattern.equals("")){
            System.out.println("Error: missing parameters.\n");
            printProgramInfo();
            System.exit(1);
        }


        if(dout.lastIndexOf(File.separator) != (dout.length() - 1)){
            dout = dout + File.separator;
        }


        CompositionMiner cm = new CompositionMiner();

        try{
            
            String dirPath = din;
            File dir = new File(dirPath);

            if(!dir.exists()){
                System.out.println("Directory does not exists: " + din);
                System.exit(1);
            }

            String[] children = dir.list();


            if(children == null){
                System.out.println("No files to mine in: " + din);
                System.exit(1);
            }

            for(int i = 0; i < children.length; i++){                
                String filename = children[i];
                String filepath = "";
                if(i >= 70) break;
                if(filename.contains("pipe")){
                    filepath = dirPath + "/" + filename;
                }else{
                    continue;
                }
                File jsonFile = new File(filepath);
                URI uri = jsonFile.toURI();
                PipesAdapter adapter = new PipesAdapter();
                adapter.loadSourceMashupModel(uri);
                cm.addCompositionToRepo(adapter.getCanonicalMashup());
            }



            System.out.println("REPOSITORY SIZE: " + cm.getRepoSize());

            /*
            minsupdf = 0.05;            
            while(minsupdf <= 0.95){
                minsupdm = 0.05;
                while(minsupdm <= 0.95){

                    //List<CanonicalMashup> patterns = cm.mineConnectorPattern(minsupdf, minsupdm);
                    //List<CanonicalMashup> patterns = cm.mineConnectorCoOccurrencePattern(minsupdf, minsupdm);
                    List<CanonicalMashup> patterns = cm.mineComponentCoOccurrencePattern(minsupdf, minsupdm, minsuppv);
                    System.out.println("MinSupDf: " + minsupdf + "\tMinSupDm: " + minsupdm + "\tMinSupPv: " + minsuppv + "\t#Patterns: " + patterns.size());

                    minsupdm = minsupdm + 0.10;
                }

                minsupdf = minsupdf + 0.10;
            }
             */

            System.out.println("MinSupDf,MinSupDm,MinSupPv,Patterns");

            minsupdf = 0.05;
            while(minsupdf <= 0.95){
                minsupdm = 0.05;
                while(minsupdm <= 0.95){
                    minsuppv = 0.05;
                    while(minsuppv <=0.95){
                    
                        List<CanonicalMashup> patterns = cm.mineComponentCoOccurrencePattern(minsupdf, minsupdm, minsuppv);
                        System.out.println(minsupdf + "," + minsupdm + "," + minsuppv + "," + patterns.size());

                        minsuppv += 0.10;
                    }

                    minsupdm = minsupdm + 0.10;
                }

                minsupdf = minsupdf + 0.10;
            }

            /*
            String prefixFName = "";
            List<CanonicalMashup> patterns = new LinkedList();
            if(pattern.equals(PARAMETERVALUE_PATTERN)){
                 patterns = cm.mineParameterValuePattern(minsuppv);
                 prefixFName = dout + "paramvaluepattern-";
            }else if(pattern.equals(CONNECTOR_PATTERN)){
                patterns = cm.mineConnectorPattern(minsupdf, minsupdm);
                prefixFName = dout + "connpattern-";
            }else if(pattern.equals(CONNECTOR_COOCCURRENCE_PATTERN)){
                patterns = cm.mineConnectorCoOccurrencePattern(minsupdf, minsupdm);
                prefixFName = dout + "conncooccurrpattern-";
            }else if(pattern.equals(COMPONENT_COOCURRENCE_PATTERN)){
                patterns = cm.mineComponentCoOccurrencePattern(minsupdf, minsupdm, minsuppv);
                prefixFName = dout + "compcooccurrpattern-";
            }

            System.out.println("MinSupDf: " + minsupdf);
            System.out.println("MinSupDm: " + minsupdm);
            System.out.println("MinSupPv: " + minsuppv);
            System.out.println("Number of patterns: " + patterns.size());

            

            
            for(int i = 0 ; i < patterns.size(); i++){
                CanonicalMashup canonical = patterns.get(i);
                //System.out.println(canonical.toString());
                canonical.serialize(prefixFName + i + ".ser");
            }
            */
            
        }catch(Exception e){
            e.printStackTrace();
        }         
    }

    private static void printProgramInfo(){
        System.out.println(
                "general usage: java -jar CompositionKnowledgeMiner.jar [COMMANDS] \n" +
                "examples: " + "\n" +
                "\t java -jar CompositionKnowledgeMiner.jar -din ./data/sport_news -dout ./data/output_patterns -p pvp -minsuppv 0.12" + "\n" +
                "\t java -jar CompositionKnowledgeMiner.jar -din ./data/sport_news -dout ./data/output_patterns -p connp -minsupdf 0.15 -minsupdm 0.25" + "\n" +
                "\t java -jar CompositionKnowledgeMiner.jar -din ./data/sport_news -dout ./data/output_patterns -p conncop -minsupdf 0.075 -minsupdm 0.25" + "\n" +
                "\t java -jar CompositionKnowledgeMiner.jar -din ./data/sport_news -dout ./data/output_patterns -p compcop -minsupdf 0.10 -minsupdm 0.10 -minsuppv 0.10" + "\n" +
                
                "COMMANDS:" + "\n" +

                "-din [directory_path]" + "\n" +
                "\t Directory that contains the pipes definition (in JSON format)" + "\n" +

                "-dout [directory_path]" + "\n" +
                "\t Directory where the patterns (as serialized Java objects) will be output" + "\n" + 

                "-p [composition_pattern_type]" + "\n" +
                "\t Type of composition pattern we want to mine. It can be one of:" + "\n" +
                "\t\t pvp: for parameter value patterns " + "\n" +
                "\t\t connp: for connector patterns" + "\n" +
                "\t\t conncop: for connector cooccurrence patterns" + "\n" +
                "\t\t compcop: for component cooccurrence patterns" + "\n" + 
                
                "-minsupdf [number]" + "\n" +
                "\t Minimum support for data flow connectors (real number between [0..1]" + "\n" + 

                "-minsupdm [number]" + "\n" +
                "\t Minimum support for data mappings (real number between [0..1]" + "\n" + 

                "-minsuppv [number]" + "\n" +
                "\t Minimum support for parameter values (real number between [0..1]" + "\n"                

                );
    }
}
